/**
 * 
 */
package de.akabahn.ebd.control.comm;

import static de.akabahn.ebd.comm.Config.FSTCTRL_EOR_CHARS;
import static de.akabahn.ebd.comm.Config.FSTCTRL_WELCOME_MSG;
import de.akabahn.ebd.comm.telnet.TelnetConnectionClient;
import de.akabahn.ebd.control.data.DebugDataContainer;

/**
 * @author Peter Eimann
 */
public class TfzInfoProvider {

	private static TfzInfoProvider instance;

	private TelnetConnectionClient telnetConnectionTfzServer;
	private DebugDataContainer ddc;

	private TfzInfoProvider(String server, int port) {
		if (server != null) {
			telnetConnectionTfzServer = new TelnetConnectionClient(server, port,
					FSTCTRL_EOR_CHARS, FSTCTRL_WELCOME_MSG);
			ddc = DebugDataContainer.getInstance();
		}
	}

	public static TfzInfoProvider getInstance(String server, int port) {
		if (instance == null) {
			instance = new TfzInfoProvider(server, port);
		}

		return instance;
	}

	public TfzInfo getTfzInfo(String tfz) {
		String response = null;

		if (telnetConnectionTfzServer != null) {
			try {
				telnetConnectionTfzServer.connect();
				telnetConnectionTfzServer.write("CMD=TFZINFO&TFZ=" + tfz);
				response = telnetConnectionTfzServer.read();
				ddc.add("TfzInfoProvider.getTfzInfo(" + tfz + ")", response);
				return TfzInfo.unmarshal(tfz, response);
			} finally {
				telnetConnectionTfzServer.disconnect();
			}
		}

		return null;
	}
}
